<template>
	<view>
		<navigationbar :title="title"></navigationbar>
		<view class="container">
			<view class="content">
				<view class="title">
					<text>教育经历</text>
				</view>
				<view class="schoolName">
					<text>学校名称</text>
					<input type="text" v-model="data.schoolname" />
				</view>
				<view class="education" @click="editEducation">
					<text>学历</text>
					<text>{{data.education}}</text>
				</view>
				<view class="specialized">
					<text>专业</text>
					<input type="text" v-model="data.specialized" />
				</view>
				<view class="time" @click="editTime">
					<text>时间段</text>
					<text>{{data.time}}</text>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="btnContainer"><u-button class="btnUploadFile" size="normal" text="保存" :color="buttonBgColor" @click="saveInfo"></u-button></view>
		</view>
		<u-picker :show="show" :columns="columns" @confirm="confirm" @cancel="cancel"></u-picker>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import navigationbar from '../../navigationBar.vue'
	export default {
		components:{
			navigationbar
		},
		created() {
			Object.assign(this.data, this.$store.state.userInfo.educate_info)
		},
		data() {
			return {
				title: '教育经历',
				buttonBgColor: '#7aaffe',
				key: 'education',
				show: false,
				columns: [['初中及以下','中专/中技','高中','本科','硕士','博士']],
				data: {
					schoolname: '',
					education: '请选择',
					specialized: '',
					time: '请选择'
				}
			}
		},
		methods: {
			confirm(data){
				this.show = false
				switch (this.key) {
					case 'education':
						this.data.education = data.value[0]
						break
					case 'time':
						let {value} = data
						this.data.time = `${value[0]}-${value[1]}`
						break
				}
			},
			cancel() {
				this.show = false
			},
			editEducation() {
				this.columns = [['初中及以下','中专/中技','高中','本科','硕士','博士']],
				this.key = 'education'
				this.show = true
			},
			editTime() {
				this.columns = [['2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022'],['2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022','2023','2024','2025','2026']]
				this.key = 'time'
				this.show = true
			},
			saveInfo() {
				if(!this.data.schoolname){
					this.$refs.uToast.show({
						message: '请填写学校名称',
						icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
						type: 'error'
					});
					return
				}
				if(!this.data.specialized){
					this.$refs.uToast.show({
						message: '请填写专业',
						icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
						type: 'error'
					});
					return
				}
				uniCloud.callFunction({
					name: 'setSchoolInfo',
					data: {
						id: this.$store.state.userID,
						...this.data
					},
					success:(res) => {
						if(res.success){
							this.$refs.uToast.show({
								message: '信息修改成功',
								icon: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
								type: 'success'
							});
							uniCloud.callFunction({
								name: 'getUserInfo',
								data: {
									id: this.$store.state.userID
								},
								success: (res) => {
									this.$store.state.userInfo = res.result.data
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	$color: #dddddd;
	.container {
		width: 100%;
		.content {
			width: 90%;
			margin: 0 auto;
			.title {
				margin-top: 40rpx;
				font-size: 60rpx;
			}
			.schoolName,.education,.specialized,.time {
				padding: 50rpx 0 ;
				border-bottom: 1rpx solid $color;
				font-size: 35rpx;
				input {
					margin-top: 30rpx;
					font-size: 40rpx;
				}
			}
			.education,.time {
				display: flex;
				flex-flow: row;
				justify-content: space-between;
			}
		}
	}
	.footer {
		width: 100%;
		margin: 0 auto;
		position: fixed;
		background-color: white;
		z-index: 9999;
		bottom: 0;
		.btnContainer {
			width: 90%;
			margin: 40rpx auto;
		}
	}

</style>
